import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '../views/login.vue'
import home from '../views/home'
import weclome from '../views/welcome.vue'
import User from '../views/user.vue'
import Rights from '../views/rights/Rights'
import Roles from '../views/rights/Roles'
import Goodscate from '../views/goods/GoodsCate'
import Goodsparams from '../views/goods/GoodsParams'
import Goodslist from '../views/goods/Goods_list'
import Goodsadd from '../views/goods/GoodsAdd'
import Order from '../views/Order.vue'
Vue.use(VueRouter)
const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: login
  },
  {
    path: '/home',
    component: home,
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        component: weclome
      },
      {
        path: '/users',
        component: User
      },
      {
        path: '/rights',
        component: Rights
      },
      {
        path: '/roles',
        component: Roles
      },
      {
        path: '/categories',
        component: Goodscate
      },
      {
        path: '/params',
        component: Goodsparams
      },
      {
        path: '/goods',
        component: Goodslist
      },
      {
        path: '/goods/add',
        component: Goodsadd
      },
      {
        path: '/orders',
        component: Order
      }
    ]
  }
]
const router = new VueRouter({
  routes
})
router.beforeEach((to, form, next) => {
  // to你想要去那个页面
  // form从哪个页面来
  // next放行
  // console.log(to)
  // console.log(form)
  const token = sessionStorage.getItem('token')
  if (token) {
    next()
  } else {
    if (to.path === '/login') return next()
    next('/login')
  }
})
export default router
